package cn.ecnu.note.controller;

import cn.ecnu.common.Permission;
import cn.ecnu.note.interceptor.Own;
import cn.ecnu.note.service.ReplyService;
import cn.ecnu.note.vo.ReplyFrontVo;
import cn.ecnu.note.vo.ReplyVo;
import cn.ecnu.note.vo.ViewReplyVo;
import cn.ecnu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/reply")
public class ReplyController {

    @Autowired
    private ReplyService replyService;

    // TODO: @Own(permission = Permission.COMMENT)
    @PostMapping("/add")
    public R addReply(@RequestBody ReplyFrontVo vo) {
        ReplyVo reply = replyService.addReply(vo);
        return R.ok().put("data", reply);
    }

    // TODO: @Own(permission = Permission.COMMENT)
    @GetMapping("/delete/{replyId}")
    public R deleteReply(@PathVariable("replyId") Integer replyId,
                         @RequestHeader("userId") Integer userId,
                         @RequestHeader("permission") Integer permission) {
        replyService.deleteReply(replyId, userId, permission);
        return R.ok();
    }

    // 根据CommentId查询Reply
    // TODO: @Own(permission = Permission.VIEW)
    @GetMapping("/getReplyByCommentId/{commentId}")
    public R getReplyByCommentId(@PathVariable("commentId") Integer commentId,
                                 @RequestParam(value = "pageNum", required = false) Integer pageNum) {
        ViewReplyVo vo = replyService.getReplyByCommentId(commentId, pageNum);
        return R.ok().put("data", vo);
    }
}
